﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Design;

namespace SharpDiary.Core.Controls
{
    public partial class PopupMessageControl : UserControl
    {
        private int _flashedTimes = 0;            //已闪烁的次数
        private readonly int MAX_FLASH_TIMES = 2; //闪烁持续的次数
        private Pen _borderPen = new Pen(Color.FromArgb(210, 210, 106), 1f); //边框画笔
        private bool _autoHide = true;
        private Timer _timerForAutoHide = new Timer();

        /// <summary>
        /// 获取或设置显示消息信息后是否自动隐藏。(设置 AutoCloseDelayTime 自动隐藏延迟的毫秒数)
        /// </summary>
        public bool AutoHide
        {
            get { return _autoHide; }
            set { _autoHide = value; }
        }

        private int _autoHideDelayTime = 3000;

        /// <summary>
        /// 获取或设置自动隐藏延迟的毫秒数。
        /// </summary>
        public int AutoHideDelayTime
        {
            get { return _autoHideDelayTime; }
            set { _autoHideDelayTime = value; }
        }

        /// <summary>
        /// 获取或设置显示文字。
        /// </summary>
        [Editor("System.ComponentModel.Design.MultilineStringEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
        [Localizable(true)]
        public string MessageText
        {
            get
            {
                return lblMessage.Text;
            }
            set
            {
                if (value == null) { return; }
                lblMessage.Text = value;
            }
        }

        #region Constructors

        public PopupMessageControl()
        {
            InitializeComponent();

            ResizeRedraw = true;
            DoubleBuffered = true;
            Visible = false;

            _timerForAutoHide.Interval = _autoHideDelayTime;
            _timerForAutoHide.Tick += new EventHandler(_timerForAutoHide_Tick);
        }

        private void _timerForAutoHide_Tick(object sender, EventArgs e)
        {
            _timerForAutoHide.Stop();
            Hide();
        }

        #endregion

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Rectangle rect = this.ClientRectangle;
            rect.Width = rect.Width - 1;
            rect.Height = rect.Height - 1;

            e.Graphics.DrawRectangle(_borderPen, rect);
        }

        /// <summary>
        /// 显示信息。
        /// </summary>
        /// <param name="text">信息</param>
        public void Show(string text)
        {
            if (text == null) { return; }

            MessageText = text;
            this.Show();
            this.BringToFront();
            Flash();
        }

        /// <summary>
        /// 闪烁。
        /// </summary>
        public void Flash()
        {
            timerFlash.Start();
        }

        protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);

            /*
            Form parent = this.FindForm();
            if (parent != null)
            {
                if (this.Visible)
                {
                    parent.Height = parent.Height + this.Height;
                }
                else
                {
                    parent.Height = parent.Height - this.Height;
                }
            }
            */
        }

        private void imageButtonClose_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right) { return; }
            this.Hide();
        }

        private void lblMessage_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right) { this.Hide(); }
        }

        private void timerFlash_Tick(object sender, EventArgs e)
        {
            if (_flashedTimes > MAX_FLASH_TIMES + 1)
            {
                timerFlash.Stop();
                _flashedTimes = 0;
                lblMessage.ForeColor = Color.FromKnownColor(KnownColor.ControlText);

                if (_autoHide)
                {
                    _timerForAutoHide.Start();
                }

                return;
            }

            _flashedTimes++;

            if (lblMessage.ForeColor == Color.FromKnownColor(KnownColor.ControlText))
            {
                lblMessage.ForeColor = Color.Gold;
            }
            else
            {
                lblMessage.ForeColor = Color.FromKnownColor(KnownColor.ControlText);
            }
        }
    }
}
